﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Bll.Logi
{ 
    /// <summary> 物流 </summary>
    public class LogisticsImportBll
    {
        public async Task<Result> SaveItems(IEnumerable<LogiTrace> items, IUser user)
        {
            if (items == null || items.Any()== false)
            {
                return new Result(false, "没有数据可以导入!");
            }

            await DB.FSql.Insert(items).ExecuteSqlBulkCopyAsync();

            return new Result(true);
        }


        public async Task<PageQueryResult<LogiTrace>> Query(DateTime begin, DateTime end, string find, int pageNum, int pageSize)
        {
            begin = begin.Date;
            end = end.LastTimeOfDay();

            var temp = await DB.FSql.Select<LogiTrace>()
                .Where(a=> a.isdel== false)
                .WhereIf(!string.IsNullOrEmpty(find), a => a.logistics_no.Contains(find) || a.TradeNo.Contains(find) 
                || a.LogisticsCompany.Contains(find))
                .Page(pageNum, pageSize)
                .Count(out var c)
                .OrderBy(a=> a.id)
                .ToListAsync();

            return new PageQueryResult<LogiTrace>(true, null, temp) { Rows= c };
        }


        public async Task<Result> Delete(IEnumerable<int> items)
        {
            if (items == null || items.Any() == false)
            {
                return new Result(true);
            }

            await DB.FSql.Update<LogiTrace>()
                .Where(a => items.Contains(a.id))
                .Set(a=> a.isdel, true)
                .ExecuteAffrowsAsync();

            return new Result(true);
        }


    }
}
